﻿using Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Configuration;

namespace MysqlEFConfig
{
    public class MysqlDbContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Career> Careers { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string connectionString = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json").Build()
                .GetConnectionString("yuanzhan");
            optionsBuilder
                .UseMySql(connectionString, ServerVersion.Parse("'5.7.38'"))
#if DEBUG
                .EnableSensitiveDataLogging()
                .LogTo(
                    //(id, level) => level == LogLevel.Error,            //过滤条件
                    (id, level) => true,            //过滤条件
                    log => Console.WriteLine(log)   //如何记录log
                 )
#endif
                .UseLazyLoadingProxies();
            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>()
                .HasIndex(u => u.LogonName)
                ;
            modelBuilder.Entity<Phone>()
                .HasIndex(u => u.Number)
                .IsUnique()
                ;
            base.OnModelCreating(modelBuilder);
        }
    }
}